﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BusinessLogicLayer;
using Entities;


namespace QuanLyTKB_TienDoGD.QuanLyLopHoc
{
    public partial class frmAddLopHoc : Office2007Form
    {
        /// <summary>
        /// Tạo đối tượng _manager
        /// </summary>
        private LopHocManager _manager;
        /// <summary>
        /// tạo đối tượng lớp học
        /// </summary>
        private LopHoc lh;
        /// <summary>
        /// tạo đối tượng frmParent từ form frmQuanLyLopHoc
        /// </summary>
        frmQuanLyLopHoc frmParent;
        /// <summary>
        /// tạo đối tượng _isUpdate
        /// </summary>
        private bool _isUpdate;
        /// <summary>
        /// Tạo form frmThemLopHoc
        /// </summary>
        public frmAddLopHoc()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Phương thức tạo lập frmThemLopHoc
        /// </summary>
        /// <param name="_man"></param>
        /// <param name="frmCha"></param>
        /// <param name="_lh"></param>
        public frmAddLopHoc(LopHocManager _man, frmQuanLyLopHoc frmCha, LopHoc _lh = null)
            : this()
        {
            _manager = _man;
            lh = _lh;
            frmParent = frmCha;
            _isUpdate = (lh != null);
        }
        /// <summary>
        /// Hàm load form frmThemLopHoc
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmThemLopHoc_Load(object sender, EventArgs e)
        {
            if(_isUpdate)
            {
                txtTenLop.Text = lh.MaLop;
                mtbSiSo.Text = lh.SiSo.ToString();
            }
            txtTenLop.ReadOnly = _isUpdate;
        }
        /// <summary>
        /// Hàm save lớp học
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            lh = new LopHoc();

            lh.MaLop = txtTenLop.Text;
            lh.SiSo = Convert.ToInt32(mtbSiSo.Text);

            var result = _manager.Validate(lh);
            if (result.Valid)
            {
                var aresult = _manager.InsertOrUpdate(lh);
                if (aresult.Success)
                {
                    frmParent.btnLamMoi.PerformClick();
                    MessageBox.Show("Them lop hoc thanh cong");
                    btnReset.PerformClick();
                }
                else MessageBox.Show(aresult.GetErrosMessages());
            }
            else MessageBox.Show(result.ToString());
        }
        /// <summary>
        /// Hàm reset
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            txtTenLop.ResetText();
            mtbSiSo.Text = "0";
        }
        /// <summary>
        /// Hàm thoát
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
